<?php

namespace App\Http\Controllers\Estate;

use App\Http\Controllers\Controller;
use App\Models\MeterSettlement;
use Illuminate\Http\Request;

class MeterSettlementController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request, MeterSettlement $meter_settlement)
    {
        $data=$request->query();
        $where=['is_deleted'=>0];
        if(!empty($data['username']))
        {
            $where[] = ['username','like',"%{$data['username']}%"];
        }
        if(!empty($data['mobile']))
        {
            $where[] = ['mobile','like',"%{$data['mobile']}%"];
        }
        if(!empty($data['community_id']))
        {
            $where['community_id'] = $data['community_id'];
        }
        if(!empty($data['change']))
        {
        $list = $meter_settlement
            ->whereDate('create_time', '>=',
                date("Y-m-d H:i:s",strtotime($data['change'][0])))
            ->whereDate('create_time', '<=',
                date("Y-m-d H:i:s",strtotime($data['change'][1])))
            ->with('community')
            ->where($where)
            ->orderBy('user_id' ,'desc')
            ->orderBy('id' ,'desc')
            ->paginate($request->per_page ?? 30);
        }else{
            $list = $meter_settlement
                ->with('community')
                ->where($where)
                ->orderBy('user_id' ,'desc')
                ->orderBy('id' ,'desc')
                ->paginate($request->per_page ?? 30);
        }
        return $this->success($list);
    }
}
